package com.redwood.report.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.redwood.report.ItemDecoration.SpaceItemDecoration;
import com.redwood.report.R;
import com.redwood.report.adapter.ReportItemAdapter;
import com.redwood.report.base.BaseActivity;
import com.redwood.report.entity.BaoBiaoBeen;
import com.redwood.report.entity.BarChartBeen;
import com.redwood.report.entity.BarDataBeen;
import com.redwood.report.entity.JtreCordInfo;
import com.redwood.report.entity.PieChartBeen;
import com.redwood.report.entity.ServiceResult;
import com.redwood.report.net.Api;
import com.redwood.report.net.NetUtils;
import com.redwood.report.util.ToastUtils;
import com.redwood.report.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sp on 2018/12/11.
 */

public class ReportActivity extends BaseActivity {

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    private BaseQuickAdapter homeAdapter;
    private String bianhao;
    private String day;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //弹出键盘滚动问题
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_report);
        unbinder = ButterKnife.bind(this);
        initView();
        initData();
        initAdapter();
    }

    private void initView() {
        String title = getIntent().getStringExtra("title");
        bianhao=getIntent().getStringExtra("bianhao");
        day=getIntent().getStringExtra("day");
        if(TextUtils.isEmpty(day)){
            day=MainActivity.day;
        }
        if(!TextUtils.isEmpty(title)) {
            tvTitle.setText(title + "报表");
        }

    }

    private void initAdapter() {
        rvList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvList.addItemDecoration(new SpaceItemDecoration(getActivity()).setSpace(0f).setSpaceColor(getResources().getColor(R.color.blueColorAppKey)));
        homeAdapter = new ReportItemAdapter(null);
        homeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(getActivity(), ReportActivity.class));
            }
        });
        homeAdapter.setEmptyView(R.layout.loading_view, (ViewGroup) rvList.getParent());
        rvList.setAdapter(homeAdapter);

        
        loadByServer();
    }

    private void loadByServer() {
        Api.jtrecord(getActivity(), bianhao, day, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if(rspData.getRecode()==1){
                    JtreCordInfo jtreCordInfo= (JtreCordInfo) rspData;
                    List<JtreCordInfo.InfoBean> infoBeanList=jtreCordInfo.getInfo();
                    if(infoBeanList!=null&&infoBeanList.size()>0) {
                        loadView(infoBeanList);
                    }else{
                        homeAdapter.setNewData(null);
                    }
                }
            }

            @Override
            public void failed(String msg) {
                ToastUtils.show(msg);
            }

            @Override
            public void onUsedCache(String msg) {

            }
        }, JtreCordInfo.class);
    }

    private void loadView(List<JtreCordInfo.InfoBean> infoBeanList) {
        List list = new ArrayList();
        BarChartBeen barChartBeen=new BarChartBeen();
        List<BarDataBeen> barDataBeenList=new ArrayList<>();
        for (int i = 0; i < infoBeanList.size(); i++) {
            JtreCordInfo.InfoBean infoBean=infoBeanList.get(i);
            int tbcolor=mContext.getResources().getColor(R.color.main_gray);
            String zhuangtai="";
            if("RUN".equals(infoBean.getZtype())){
                zhuangtai=getResources().getString(R.string.yunxing);
                tbcolor=mContext.getResources().getColor(R.color.main_green);
            }else if("FREE".equals(infoBean.getZtype())||"MOULD".equals(infoBean.getZtype())){
                zhuangtai=getResources().getString(R.string.daiji);
                tbcolor=mContext.getResources().getColor(R.color.main_yellow);
            }else if("BAD".equals(infoBean.getZtype())){
                zhuangtai=getResources().getString(R.string.guzhang);
                tbcolor=mContext.getResources().getColor(R.color.main_red);
            }
            BarDataBeen barDataBeen = new BarDataBeen();
            barDataBeen.setColor(tbcolor);
            float timer=Utils.intFloatTuBiao(infoBean.getMiaoshu());
            if(timer<0.05f){
                timer=0.05f;
            }
            barDataBeen.setTimer(timer);
            barDataBeenList.add(barDataBeen);
        }
        barChartBeen.setBarDataBeenList(barDataBeenList);
        list.add(barChartBeen);
        BaoBiaoBeen baoBiaoBeenTitle = new BaoBiaoBeen();
        baoBiaoBeenTitle.setIsTitle(true);
        list.add(baoBiaoBeenTitle);
        PieChartBeen pieChartBeen=new PieChartBeen();
        int zaixianmaoshu=0;
        int lixianmaoshu=0;
        int guzhangmaoshu=0;
        int daijimaoshu=0;
        for (int i = 0; i < infoBeanList.size(); i++) {
            JtreCordInfo.InfoBean infoBean=infoBeanList.get(i);
            BaoBiaoBeen baoBiaoBeen = new BaoBiaoBeen();
            baoBiaoBeen.setStartTimer(Utils.timertoHour(infoBean.getStarttimer()));
            baoBiaoBeen.setEndTimer(Utils.timertoHour(infoBean.getEndtimer()));
            baoBiaoBeen.setLongTimer(Utils.getStingToHour2(infoBean.getMiaoshu()));
            String zhuangtai="";
            if("RUN".equals(infoBean.getZtype())){
                zhuangtai=getResources().getString(R.string.yunxing);
                zaixianmaoshu=zaixianmaoshu+infoBean.getMiaoshu();
            }else if("FREE".equals(infoBean.getZtype())||"MOULD".equals(infoBean.getZtype())){
                zhuangtai=getResources().getString(R.string.daiji);
                daijimaoshu=daijimaoshu+infoBean.getMiaoshu();
            }else if("BAD".equals(infoBean.getZtype())){
                zhuangtai=getResources().getString(R.string.guzhang);
                guzhangmaoshu=guzhangmaoshu+infoBean.getMiaoshu();
            }
            baoBiaoBeen.setZhuangtai(zhuangtai);
            baoBiaoBeen.setYuanying(infoBean.getYuanying());
            baoBiaoBeen.setCaozuoren(infoBean.getCaozuo());
            list.add(baoBiaoBeen);

        }
        int allCount=zaixianmaoshu+lixianmaoshu+guzhangmaoshu+daijimaoshu;
        pieChartBeen.setDaiji((float) daijimaoshu/allCount);
        pieChartBeen.setLixian((float) lixianmaoshu/allCount);
        pieChartBeen.setJingdao((float) guzhangmaoshu/allCount);
        pieChartBeen.setZaixian((float) zaixianmaoshu/allCount);
        list.add(pieChartBeen);
        homeAdapter.setNewData(list);
    }


    private void initData() {


    }


}
